草庐IT

AudioToolBox 解码AAC

全部标签

gob 编码到/从 *os.File 解码不工作

当我使用文件指针时f*os.File我得到一个空映射funcdecode(f*os.File,bmap[string]interface{})error{err:=gob.NewDecoder(f).Decode(&b)fmt.Printf("%+v\n",b)returnerr}funcencode(f*os.File,bmap[string]interface{})error{bb:=map[string]interface{}{"X":1,"Greeting":"hello",}err:=gob.NewEncoder(f).Encode(bb)f.Sync()//fmt.Prin

go - 将 JSON 嵌套字段值解码为结构字段

//Giventhefollowingstruct:typeMyStructstruct{FirststringSecondstringThirdstring}//IwouldliketounmarshalthefollowingJSONintoMyStructsuchas:bytes:=[]byte({{"first":{"href":"http://some/resources/1"},"second":{"href":"http://http://some/resources/2"},"third":{"href":"http://some/resources/3"}})vars

json - 在 Go 中,为什么 JSON null 有时不会传递给 UnmarshalJSON 进行解码?

Go提供了encoding/json.Unmarshaler接口(interface),因此类型可以控制它们从JSON解码的方式。在几乎所有情况下,编码后的JSON值都直接传递给UnmarshalJSON方法,但如果Unmarshaler是一个指针并且JSON值为null。在这种情况下,指针设置为nil而根本不调用UnmarshalJSON。这是一个例子:packagemainimport("encoding/json""fmt")typeTstringfunc(v*T)UnmarshalJSON(b[]byte)error{ifb[0]=='n'{*v="null"}else{*v=

json - 如何使用 Golang func json.Unmarshal 解码这个复杂的 Json 数据?

我有这样的json对象:{"action":"GetLoad","resource_id":"lb-cdvyel0v","ret_code":0,"meter_set":[{"data_set":[{"data":[[1478672400,[1,0]],[1,0],[0,0],[8,0],[1,0]],"eip_id":"eip-jf79ljt7"},{"data":[[1478693280,[0,0]],[1,0],[0,0]],"eip_id":"eip-mw6n6wg0"}],"meter_id":"uaffic"}]}我尝试这样解决问题:typeCommonResponsest

json - 在结构映射中存储/解码 json 的最佳方式

我在结构中有一个映射如下:typeRedstruct{**otherTelmap[string]string`json:"Tel"`}我收到的数据json格式如下{"Params":[{"rewew":"tref"},{"Value":"x"},....]}我正在寻找用数据填充我的结构的最有效方法,以便Tel["rewew"]="tref"Tel["Value"]="x"对于其余值,当执行此操作时这些值更简单时,它可以正常工作:vartReddecode:=json.NewDecoder(req.Body)decode.Decode(&t)但是我在使用map时遇到了问题

json - 将纪元时间戳解码为 time.Time

使用此结构运行Decode()会产生“时间戳”列错误:typeMetricsstruct{Idint`orm:"column(id);auto"`Namestring`orm:"column(name);size(255);null"json:"metric_name"`json:"lon"`Timestamptime.Time`orm:"column(timestamp);type(datetime)"json:"timestamp;omitempty"`}错误:parsingtime"1352289160"as""2006-01-02T15:04:05Z07:00"":cannot

go - 在 Go 中解码 URL

我调用的API返回UTF-8编码的XML文档中的URL。解析返回类似http://www.test.com的内容,我想将其转换为http://www.test.com.我正在努力寻找正确的方法来做到这一点。任何帮助将不胜感激!编辑:这段代码可以满足我的需要,但本以为会有一个预构建的函数来做类似于这个网站的事情:https://www.url-encode-decode.com/for_,user:=rangex.Users{a:=strings.Replace(user.Username,":",":",-1)b:=strings.Replace(a,"/","/"

json - 为非内置类型定义自定义解码

我们大多数人都知道可以使用JSON标签解码JSON对象:varjsonData=`{"name":"BrownBear"}`typeElephantstruct{Namestring`json:"name"`}这是因为string是内置类型。但是,如果Name不是内置类型,而我们想在不同的结构中使用这种类型怎么办?varjsonData=`{"name":"BrownBear"}`typeElephantstruct{NameName`json:"name"`//Unmarshallingfailshere}typeFelinestruct{NameName`json:"name"`/

go - 如何将 json 解码为 [] 人?

我正在尝试动态设置一个类型为interface{}的字段。在下面的所有情况下,json解码为正确的结构,但在“问题”情况下,json解码为[]interface{}。对于那种情况,我期待[]Person。为什么我得到错误的“问题”类型?packagemainimport("encoding/json""fmt""reflect")typeEmployeesstruct{Indicatorstring`json:"indicator"`Itemsinterface{}`json:"items"`}typePersonstruct{Namestring`json:"name"`}funcm

json - 为什么我的 Go 服务器不能正确解码从客户端发送的 JSON?

我正在为一个项目用Go语言编写服务器,其中涉及从客户端接收JSON数据并发回JSON响应。当我运行代码时,我发出的任何请求都可以正常工作,但响应始终为空。这是我的服务器的代码。typeAddPlayerDatastruct{namestring}funcmain(){router:=mux.NewRouter()router.HandleFunc("/",func(whttp.ResponseWriter,r*http.Request){fmt.Println("[SUCCESS]Requestfrom",r.RemoteAddr)decoder:=json.NewDecoder(r.